/*
  分页数据源
*/
function pageData() {
    var curPage = parseInt($("#cupage").val());
    var total = parseInt($("#total").val());
    var html_lis = "<ul class='pagination'>";
    var ik = 5;
    if (total <= 5) {
        if (total == 1) {
            html_lis += "<li class='prev disabled' id='befora'> <a href='javascript:void(0);'>首页</a><a href='javascript:void(0)'>上一页</a></li>";
            html_lis += "<li class='active'><a href='javascript:void(0)'>" + curPage + "</a></li>";
            html_lis += "<li class='next disabled' id='b'><a href='javascript:void(0)'>下一页</a><a href='javascript:void(0)'>末页 </a></li></ul>";
        }
        else if (total < 1) {
            $("#currentpage").html(0);
            html_lis += "<li class='prev disabled' id='befora'> <a href='javascript:void(0);'>首页</a><a href='javascript:void(0)'>上一页</a></li>";
            html_lis += "<li class='next disabled' id='b'><a href='javascript:void(0)'>下一页</a><a href='javascript:void(0)'>末页 </a></li></ul>";
        }
        else {
            ik = total;
            html_lis += page_list(ik, curPage, total);
        }
    }
    else {
        html_lis += page_list(ik, curPage, total);
    }
    $("#datatable").html(html_lis);
}

/*
 分页列表
*/
function page_list(ik, curPage, total) {
    var html_li = "";
    if (curPage == 1) {
        html_li += "<li class='prev disabled' id='befora'> <a href='javascript:void(0);'>首页</a><a href='javascript:void(0)'>上一页</a></li>";
        for (var k = 1; k <= ik; k++) {
            if (k == 1) {
                html_li += "<li class='active'><a href='javascript:void(0)' onclick=$('#cupage').val(" + curPage + ");loadData();>" + curPage + "</a></li>";
            }
            else {
                html_li += "<li><a href='javascript:void(0)' onclick=$('#cupage').val(" + (curPage + k - 1) + ");loadData();>" + (curPage + k - 1) + "</a></li>";
            }
        }
        html_li += " <li class='next' id='b'><a href='javascript:void(0)' onclick=$('#cupage').val(parseInt($('#cupage').val())+1);loadData();>下一页</a><a href='javascript:void(0)' onclick=$('#cupage').val(" + total + ");loadData();>末页 </a></li></ul>";
    }
    if (curPage > 1 && curPage < total) {
        html_li += "<li class='prev' id='befora'> <a href='javascript:void(0)'  onclick=$('#cupage').val(1);loadData();>首页</a><a href='javascript:void(0)'  onclick=$('#cupage').val(parseInt($('#cupage').val())-1);loadData();>上一页</a></li>";
        if (curPage == 2) {
            for (var k = 1; k <= ik; k++) {
                if (k == 1) {
                    html_li += "<li><a href='javascript:void(0)' onclick=$('#cupage').val(" + (curPage - 1) + ");loadData();>" + (curPage - 1) + "</a></li>";
                }
                else if (k == 2) {
                    html_li += "<li class='active'><a href='javascript:void(0)' onclick=$('#cupage').val(" + curPage + ");loadData();>" + curPage + "</a></li>";
                }
                else {
                    html_li += "<li><a href='javascript:void(0)' onclick=$('#cupage').val(" + (curPage + k - 2) + ");loadData();>" + (curPage + k - 2) + "</a></li>";
                }
            }
        }
        else if (curPage == (total - 1)) {
            for (var k = 1; k <= ik; k++) {
                if (k == 1) {
                    html_li += "<li><a href='javascript:void(0)' onclick=$('#cupage').val(" + (curPage - (ik - 2)) + ");loadData();>" + (curPage - (ik - 2)) + "</a></li>";
                }
                else if (k == 4) {
                    html_li += "<li class='active'><a href='javascript:void(0)' onclick=$('#cupage').val(" + curPage + ");loadData();>" + curPage + "</a></li>";
                }
                else {
                    html_li += "<li><a href='javascript:void(0)' onclick=$('#cupage').val(" + (curPage + k - 4) + ");loadData();>" + (curPage + k - 4) + "</a></li>";
                }
            }
        }
        else {
            for (var k = 1; k <= ik; k++) {
                if (k < 3) {
                    html_li += "<li><a href='javascript:void(0)' onclick=$('#cupage').val(" + (curPage - (ik - k - 2)) + ");loadData();>" + (curPage - (ik - k - 2)) + "</a></li>";
                }
                else if (k == 3) {
                    html_li += "<li class='active'><a href='javascript:void(0)' onclick=$('#cupage').val(" + curPage + ");loadData();>" + curPage + "</a></li>";
                }
                else {
                    html_li += "<li><a href='javascript:void(0)' onclick=$('#cupage').val(" + (curPage + (k - 3)) + ");loadData();>" + (curPage + (k - 3)) + "</a></li>";
                }
            }
        }
        html_li += "<li class='next' id='b'><a   href='javascript:void(0)' onclick=$('#cupage').val(parseInt($('#cupage').val())+1);loadData();>下一页</a><a href='javascript:void(0)' onclick =$('#cupage').val(" + total + ");loadData();>末页 </a></li></ul>";
    }
    if (curPage == total) {
        html_li += "<li class='prev' id='befora'> <a href='javascript:void(0)' onclick =$('#cupage').val(1);loadData();>首页</a><a href='javascript:void(0)' onclick =$('#cupage').val(parseInt($('#cupage').val())-1);loadData();>上一页</a></li>";
        for (var k = 1; k <= ik; k++) {
            if (k == ik) {
                html_li += "<li class='active'><a href='javascript:void(0)' onclick=$('#cupage').val(" + curPage + ");loadData();>" + curPage + "</a></li>";
            }
            else {
                html_li += "<li><a href='javascript:void(0)' onclick=$('#cupage').val(" + (curPage - (ik - k)) + ");loadData();>" + (curPage - (ik - k)) + "</a></li>";
            }
        }
        html_li += "<li class='next disabled' id='b'><a href='javascript:void(0)'>下一页</a><a href='javascript:void(0)'>末页 </a></li></ul>";
    }
    return html_li;
}

/*
  为分页赋值
*/
function pageValue(data) {
    $("#totalrows").html(data.page.totalRow);
    $("#numperpage").html(data.page.pageSize);
    $("#currentpage").html(data.page.pageNumber);
    $("#totalpages").html(data.page.totalPage);
    $("#cupage").val(data.page.pageNumber);
    $("#total").val(data.page.totalPage);
}